require(["common", "jQuery", "coAjax", "queryString", "hash_routie", "serverNotify"], function(jSouper, $, coAjax, QueryString, hash_routie, serverNotify) {
	/*
	 * Sock连接
	 */
	var user_notify = serverNotify("user");
	//订单支付成功后的页面回调
	function _pay_success_cb(new_ach_info) {
		App.set("user.pay_ach", new_ach_info);
		href.jump("/mobile.personal.html");
	}
	user_notify.on("pay_success", _pay_success_cb);

	user_notify.on("ach.state.changed", function(ach_info) {
		var achs = App.get("user.achs");
		jSouper.forEach(achs, function(ach, index) {
			if (ach._id === ach_info.ach_id) {
				App.set("user.achs." + index + ".info.state", ach_info.state);
			}
		});
	});


	var _num = 10;
	var _page = 0;
	var _type;

	function on_cancel_ach_success(vm, result) {
		vm.removeFromArray();
		_load_achs_page(_type, _num, _page);
	};

	function _load_achs_page(type, num, page) {
		App.model.toggle('isloading');
		coAjax.get(appConfig.user.achs_with_type_and_page_info, {
			num: num,
			page: page,
			type: type
		}, function(result) {
			var info = result.result;
			jSouper.forEach(info.achs, function(ach) {
				$Event = ach.$Event || (ach.$Event = {});
				$Event.cancel_ach_success = on_cancel_ach_success;
			});
			App.concat("user.achs", info.achs);
			if (!info.achs.length) {
				App.model.toggle('ismore');
			};
			page_info = {
				total_num: info.total_num,
				total_page: info.total_page,
				page: info.page,
				num: info.num
			};
			page_info._number_list = [];
			page_info._number_list.length = info.total_page;
			App.set("$Cache.user.achs.page_info", page_info);
			App.model.toggle('isloading');
		}, function(errorCode, xhr, errorMsg) {
			console.log(errorCode);
			alert("error", errorMsg);
		});
	};
	hash_routie.on("order", function() {
		App.set("thisPage", 4);
		var queryString = new QueryString(location.hash);
		_num = queryString.get("num") || _num;
		_page = queryString.get("page") || _page;
		_type = queryString.get("type") || _type;

		App.set('title', '订单管理')

		_load_achs_page(_type, _num, _page);
	});
	//分页功能
	App.set('$Event.get_more_order', function() {
		_page += 1;
		_load_achs_page(_type, _num, _page);
	});

	App.set('$Event.pays.confirm_goods', function(e, vm) {
		var url;
		var state = vm.get("info.state");
		if (state == appConfig.ACH_STATE["ol未收货"]) {
			url = appConfig.user["ach_ol未收货_to_ol交易完成"]
		} else if (state == appConfig.ACH_STATE["已发货"]) {
			url = appConfig.user["ach_已发货_to_未收货未付款"]
		} else {
			alert("error", "错误的订单状态")
			return;
		}
		coAjax.put(url + vm.get("_id"), function(result) {
			alert("success", "成功确认收获");
			console.log(result.result);
			vm.set("info.state", result.result);
		});
	})
	App.set("$Event.pays.lock_ach", function(e, vm) {
		coAjax.put(appConfig.user.ach_lock + vm.get("_id"), function(result) {
			vm.set(result.result);
		});
	});
	App.set("$Event.pays.cancel_ach", function(e, vm) {
		require("coAjax")["delete"](appConfig.user.ach_cancel + vm.get("_id"), function(result) {
			alert("success", "订单取消成功");
			vm.removeFromArray();
			_load_achs_page(_type, _num, _page);
		});
	});
});